package com.yanqu.road.server.cmd.activity.jiuzhou;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.jiuzhou.CrossJiuZhouMgr;
import com.yanqu.road.server.manager.activity.jiuzhou.activity.CrossJiuZhouActivity;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_CROSS_JIU_ZHOU_SILVER_BUY_BUFF, desc = "银两鼓舞")
public class CrossJiuZhouSilverBuyBuffCmd extends CrossCommand {

    private int activityId;

    private int roomId;

    private long opponentUserId;

    private String cost;

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossJiuZhouProto.CrossJiuZhouSilverBuyBuffReqMsg reqMsg = CrossJiuZhouProto.CrossJiuZhouSilverBuyBuffReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        roomId = reqMsg.getRoomId();
        opponentUserId = reqMsg.getOpponentUserId();
        cost = reqMsg.getCost();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossJiuZhouActivity activity = CrossJiuZhouMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_JIU_ZHOU_ACTIVITY_NOT_EXIST;
            return;
        }

        ret = activity.silverBuyBuffByCmd(playerId, roomId, opponentUserId, cost);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            CrossJiuZhouProto.CrossJiuZhouSilverBuyBuffRespMsg.Builder builder = CrossJiuZhouProto.CrossJiuZhouSilverBuyBuffRespMsg.newBuilder();
            builder.setRet(ret);
            builder.setCost(cost);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_JIU_ZHOU_SILVER_BUY_BUFF_FROM_CROSS, builder));
        }
    }
}
